Load all required libraries.

library(tidyverse)
## -- Attaching packages --------------------------------------- tidyverse 1.3.1 --
## v ggplot2 3.3.3     v purrr   0.3.4
## v tibble  3.1.1     v dplyr   1.0.6
## v tidyr   1.1.3     v stringr 1.4.0
## v readr   1.4.0     v forcats 0.5.1
## -- Conflicts ------------------------------------------ tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(broom)

Read in raw data from RDS.

raw_data <- readRDS("./n1_n2_cleaned_cases.rds")

Make a few small modifications to names and data for visualizations.

final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
  rename(Facility = wrf) %>%
  mutate(Facility = recode(Facility, 
                           "NO" = "WRF A",
                           "MI" = "WRF B",
                           "CC" = "WRF C"))

Seperate the data by gene target to ease layering in the final plot

#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>% 
  select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke)) %>%
  group_by(date) %>% summarise_if(is.numeric, mean)

#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]

only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]

Build the main plot

      #first layer is the background epidemic curve
        p1 <- only_background %>%
              plotly::plot_ly() %>%
              plotly::add_trace(x = ~date, y = ~new_cases_clarke, 
                                type = "bar", 
                                hoverinfo = "text",
                                text = ~paste('</br> Date: ', date,
                                                     '</br> Daily Cases: ', new_cases_clarke),
                                alpha = 0.5,
                                name = "Daily Reported Cases",
                                color = background_color,
                                colors = background_color,
                                showlegend = FALSE) %>%
            layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #renders the main plot layer two as seven day moving average
        p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke, 
                             type = "scatter",
                             mode = "lines",
                             hoverinfo = "text",
                            text = ~paste('</br> Date: ', date,
                                                     '</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
                             name = "Seven Day Moving Average Athens",
                             line = list(color = seven_day_ave_color),
                             showlegend = FALSE)
      

        
        #renders the main plot layer three as positive target hits
        
        p2 <- plotly::plot_ly() %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n1,
                                       symbol = ~Facility,
                                       marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
          plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
                                       type = "scatter",
                                       mode = "markers",
                                       hoverinfo = "text",
                                       text = ~paste('</br> Date: ', date,
                                                     '</br> Facility: ', Facility,
                                                     '</br> Target: ', target,
                                                     '</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
                                       data = only_n2,
                                       symbol = ~Facility,
                                       marker = list(color = '#D95F02', size = 8, opacity = 0.65),
                                       showlegend = FALSE) %>%
            layout(yaxis = list(title = "SARS CoV-2 Copies/L", 
                                 showline = TRUE,
                                 type = "log",
                                 dtick = 1,
                                 automargin = TRUE)) %>%
            layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
        
        #adds the limit of detection dashed line
        p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"), 
                                          xend = ~max(date + 10), 
                                          y = 3571.429, yend = 3571.429,
                                          opacity = 0.35,
                                          line = list(color = "black", dash = "dash")) %>%
          layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y", 
                                    text = "Limit of Detection", showarrow = FALSE))

        

        p1
        p2

Combine the two main plot pieces as a subplot

#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")

#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")


#rejoin the old data frames then seperate in to averages for each plant. 
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
  select(c(date, mean_total_copies)) %>%
  group_by(date) %>%
  summarize_if(is.numeric, mean) %>%
  ungroup() %>%
  mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "new_cases_clarke", "cases_cum_clarke", "X7_day_ave_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "day", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)

Build loess smoothing figures figures

This makes the individual plots

#**************************************WRF A PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77', 
              span = 0.6, n = 359)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'

fit_botha
##   [1] 13.05405 13.04927 13.04456 13.03992 13.03535 13.03084 13.02640 13.02202
##   [9] 13.01769 13.01343 13.00922 13.00506 13.00095 12.99689 12.99287 12.98890
##  [17] 12.98497 12.98108 12.97723 12.97341 12.96962 12.96586 12.96214 12.95844
##  [25] 12.95476 12.95110 12.94747 12.94385 12.94025 12.93666 12.93308 12.92951
##  [33] 12.92594 12.92238 12.91883 12.91527 12.91171 12.90816 12.90462 12.90110
##  [41] 12.89759 12.89410 12.89063 12.88719 12.88378 12.88040 12.87706 12.87376
##  [49] 12.87049 12.86728 12.86411 12.86099 12.85793 12.85492 12.85198 12.84910
##  [57] 12.84629 12.84355 12.84088 12.83829 12.83578 12.83335 12.83101 12.82876
##  [65] 12.82661 12.82455 12.82258 12.82073 12.81897 12.81733 12.81580 12.81431
##  [73] 12.81280 12.81127 12.80974 12.80820 12.80666 12.80513 12.80361 12.80210
##  [81] 12.80062 12.79916 12.79774 12.79636 12.79502 12.79373 12.79249 12.79131
##  [89] 12.79020 12.78916 12.78819 12.78731 12.78651 12.78580 12.78519 12.78469
##  [97] 12.78429 12.78401 12.78384 12.78380 12.78389 12.78412 12.78448 12.78499
## [105] 12.78565 12.78647 12.78739 12.78833 12.78931 12.79033 12.79139 12.79249
## [113] 12.79363 12.79482 12.79607 12.79737 12.79873 12.80015 12.80163 12.80317
## [121] 12.80479 12.80648 12.80824 12.81008 12.81200 12.81400 12.81609 12.81826
## [129] 12.82053 12.82290 12.82536 12.82792 12.83059 12.83336 12.83624 12.84023
## [137] 12.84617 12.85384 12.86303 12.87352 12.88509 12.89752 12.91060 12.92410
## [145] 12.93781 12.95151 12.96498 12.97801 12.99037 13.00184 13.01222 13.02127
## [153] 13.02879 13.03455 13.04099 13.05039 13.06232 13.07636 13.09207 13.10903
## [161] 13.12680 13.14496 13.16308 13.18072 13.19746 13.21287 13.22651 13.23797
## [169] 13.24680 13.25258 13.25706 13.26224 13.26803 13.27434 13.28111 13.28823
## [177] 13.29565 13.30326 13.31100 13.31878 13.32651 13.33413 13.34154 13.34866
## [185] 13.35541 13.36172 13.36750 13.37266 13.37713 13.38083 13.38367 13.38558
## [193] 13.38646 13.38624 13.38485 13.38218 13.37818 13.37275 13.36565 13.35681
## [201] 13.34638 13.33450 13.32133 13.30702 13.29170 13.27554 13.25868 13.24128
## [209] 13.22348 13.20542 13.18727 13.16917 13.15127 13.13372 13.11667 13.10027
## [217] 13.08466 13.06772 13.04743 13.02412 12.99814 12.96982 12.93949 12.90750
## [225] 12.87418 12.83986 12.80487 12.76957 12.73428 12.69934 12.66508 12.63184
## [233] 12.59996 12.56977 12.54161 12.51582 12.49273 12.47268 12.45327 12.43208
## [241] 12.40939 12.38547 12.36059 12.33505 12.30910 12.28303 12.25711 12.23163
## [249] 12.20685 12.18306 12.16053 12.13954 12.12036 12.10328 12.08817 12.07463
## [257] 12.06251 12.05167 12.04195 12.03320 12.02528 12.01803 12.01129 12.00493
## [265] 11.99879 11.99272 11.98657 11.98018 11.97342 11.96613 11.95815 11.94934
## [273] 11.93955 11.92999 11.92184 11.91490 11.90898 11.90387 11.89938 11.89531
## [281] 11.89147 11.88766 11.88367 11.87931 11.87439 11.86870 11.86205 11.85424
## [289] 11.84507 11.83538 11.82610 11.81718 11.80857 11.80023 11.79209 11.78411
## [297] 11.77624 11.76843 11.76062 11.75277 11.74483 11.73674 11.72846 11.71993
## [305] 11.71111 11.70194 11.69237 11.68236 11.67224 11.66236 11.65268 11.64316
## [313] 11.63376 11.62442 11.61511 11.60578 11.59639 11.58690 11.57727 11.56744
## [321] 11.55738 11.54705 11.53639 11.52538 11.51414 11.50286 11.49152 11.48012
## [329] 11.46865 11.45712 11.44551 11.43382 11.42205 11.41018 11.39822 11.38616
## [337] 11.37399 11.36171 11.34931 11.33679 11.32414 11.31136 11.29844 11.28530
## [345] 11.27187 11.25818 11.24423 11.23007 11.21570 11.20116 11.18645 11.17162
## [353] 11.15666 11.14162 11.12651 11.11135 11.09616 11.08097 11.06580
#assign fits to a vector
both_trenda <- fit_botha

#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax

#reassign dataframes (just to be safe)
work_botha <- wrfa_both

#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date

#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
                    data = smooth_frame_botha,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha,
                                  '</br> Median Log Copies: ', round(both_trenda, digits = 2)),
                    line = list(color = '#1B9E77', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
                                  '</br> Min Log Copies: ', round(both_ymina, digits = 2)),
                    name = "",
                    fillcolor = '#1B9E77',
                    line = list(color = '#1B9E77')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF A") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_ymina), yend = ~max(both_ymaxa),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfa_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#1B9E77', size = 6, opacity = 0.65))

p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines 
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02', 
              span = 0.6, n = 359)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'

fit_bothb
##   [1] 12.63062 12.62574 12.62097 12.61630 12.61173 12.60726 12.60288 12.59860
##   [9] 12.59441 12.59031 12.58631 12.58239 12.57856 12.57482 12.57117 12.56760
##  [17] 12.56412 12.56071 12.55739 12.55415 12.55099 12.54790 12.54489 12.54196
##  [25] 12.53910 12.53631 12.53359 12.53094 12.52836 12.52585 12.52340 12.52102
##  [33] 12.51870 12.51644 12.51425 12.51211 12.51003 12.50800 12.50602 12.50410
##  [41] 12.50224 12.50045 12.49871 12.49705 12.49545 12.49392 12.49247 12.49110
##  [49] 12.48980 12.48859 12.48746 12.48641 12.48546 12.48460 12.48383 12.48316
##  [57] 12.48259 12.48212 12.48176 12.48150 12.48136 12.48132 12.48140 12.48160
##  [65] 12.48191 12.48235 12.48291 12.48360 12.48442 12.48537 12.48646 12.48770
##  [73] 12.48910 12.49066 12.49238 12.49426 12.49628 12.49845 12.50076 12.50320
##  [81] 12.50578 12.50849 12.51132 12.51428 12.51735 12.52053 12.52382 12.52722
##  [89] 12.53072 12.53432 12.53801 12.54179 12.54566 12.54960 12.55363 12.55773
##  [97] 12.56189 12.56613 12.57042 12.57477 12.57918 12.58363 12.58813 12.59268
## [105] 12.59726 12.60187 12.60669 12.61188 12.61740 12.62323 12.62934 12.63571
## [113] 12.64231 12.64911 12.65609 12.66321 12.67046 12.67780 12.68520 12.69265
## [121] 12.70011 12.70755 12.71495 12.72229 12.72953 12.73664 12.74361 12.75040
## [129] 12.75699 12.76334 12.76944 12.77526 12.78076 12.78678 12.79406 12.80248
## [137] 12.81191 12.82220 12.83324 12.84488 12.85700 12.86946 12.88213 12.89488
## [145] 12.90758 12.92009 12.93228 12.94402 12.95517 12.96561 12.97520 12.98381
## [153] 12.99131 12.99757 13.00518 13.01648 13.03093 13.04800 13.06715 13.08786
## [161] 13.10958 13.13177 13.15392 13.17547 13.19590 13.21467 13.23125 13.24510
## [169] 13.25568 13.26247 13.26733 13.27245 13.27778 13.28326 13.28883 13.29444
## [177] 13.30003 13.30556 13.31095 13.31616 13.32113 13.32581 13.33014 13.33406
## [185] 13.33752 13.34046 13.34283 13.34457 13.34563 13.34595 13.34548 13.34415
## [193] 13.34192 13.33873 13.33452 13.32924 13.32283 13.31524 13.30526 13.29197
## [201] 13.27571 13.25682 13.23565 13.21254 13.18784 13.16188 13.13501 13.10758
## [209] 13.07992 13.05239 13.02532 12.99905 12.97394 12.95033 12.92856 12.90896
## [217] 12.89190 12.87469 12.85464 12.83204 12.80721 12.78043 12.75201 12.72226
## [225] 12.69146 12.65993 12.62796 12.59586 12.56392 12.53245 12.50174 12.47210
## [233] 12.44383 12.41723 12.39259 12.37023 12.35044 12.33351 12.31782 12.30157
## [241] 12.28489 12.26788 12.25067 12.23337 12.21609 12.19895 12.18206 12.16554
## [249] 12.14950 12.13406 12.11932 12.10542 12.09245 12.08054 12.07040 12.06252
## [257] 12.05668 12.05263 12.05015 12.04900 12.04895 12.04977 12.05122 12.05308
## [265] 12.05510 12.05707 12.05874 12.05988 12.06026 12.05965 12.05781 12.05452
## [273] 12.04953 12.04422 12.03997 12.03664 12.03407 12.03208 12.03051 12.02922
## [281] 12.02803 12.02678 12.02531 12.02346 12.02106 12.01796 12.01400 12.00901
## [289] 12.00282 11.99594 11.98895 11.98189 11.97475 11.96755 11.96031 11.95304
## [297] 11.94576 11.93848 11.93121 11.92396 11.91676 11.90961 11.90253 11.89554
## [305] 11.88864 11.88185 11.87519 11.86867 11.86231 11.85613 11.85011 11.84421
## [313] 11.83842 11.83271 11.82705 11.82143 11.81582 11.81019 11.80453 11.79881
## [321] 11.79300 11.78708 11.78103 11.77482 11.76855 11.76233 11.75615 11.74999
## [329] 11.74387 11.73776 11.73167 11.72558 11.71949 11.71339 11.70727 11.70113
## [337] 11.69496 11.68875 11.68250 11.67620 11.66985 11.66342 11.65693 11.65035
## [345] 11.64369 11.63695 11.63015 11.62328 11.61637 11.60941 11.60242 11.59541
## [353] 11.58838 11.58135 11.57431 11.56729 11.56029 11.55332 11.54639
#assign fits to a vector
both_trendb <- fit_bothb

#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax

#reassign dataframes (just to be safe)
work_bothb <- wrfb_both

#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date

#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
                    data = smooth_frame_bothb,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb,
                                  '</br> Median Log Copies: ', round(both_trendb, digits = 2)),
                    line = list(color = '#D95F02', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminb, digits = 2)),
                    name = "",
                    fillcolor = '#D95F02',
                    line = list(color = '#D95F02')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF B") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminb), yend = ~max(both_ymaxb),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfb_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#D95F02', size = 6, opacity = 0.65))

p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")

#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************

extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) + 
  stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A', 
              span = 0.6, n = 359)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'

fit_bothc
##   [1] 12.05959 12.05281 12.04614 12.03958 12.03313 12.02678 12.02055 12.01441
##   [9] 12.00838 12.00245 11.99661 11.99087 11.98522 11.97966 11.97419 11.96881
##  [17] 11.96351 11.95830 11.95317 11.94811 11.94314 11.93823 11.93340 11.92864
##  [25] 11.92395 11.91933 11.91477 11.91027 11.90584 11.90146 11.89714 11.89287
##  [33] 11.88865 11.88449 11.88037 11.87630 11.87228 11.86829 11.86435 11.86044
##  [41] 11.85657 11.85274 11.84893 11.84518 11.84149 11.83787 11.83432 11.83085
##  [49] 11.82745 11.82413 11.82090 11.81775 11.81468 11.81170 11.80881 11.80602
##  [57] 11.80332 11.80072 11.79822 11.79582 11.79353 11.79134 11.78927 11.78730
##  [65] 11.78545 11.78372 11.78210 11.78061 11.77924 11.77800 11.77688 11.77590
##  [73] 11.77505 11.77434 11.77376 11.77332 11.77303 11.77288 11.77274 11.77247
##  [81] 11.77210 11.77163 11.77108 11.77046 11.76980 11.76910 11.76838 11.76765
##  [89] 11.76693 11.76624 11.76558 11.76498 11.76445 11.76400 11.76364 11.76340
##  [97] 11.76329 11.76332 11.76350 11.76386 11.76440 11.76515 11.76611 11.76730
## [105] 11.76874 11.77044 11.77241 11.77468 11.77724 11.78013 11.78335 11.78693
## [113] 11.79086 11.79528 11.80025 11.80576 11.81175 11.81820 11.82507 11.83233
## [121] 11.83993 11.84785 11.85604 11.86448 11.87312 11.88194 11.89089 11.89994
## [129] 11.90906 11.91821 11.92735 11.93644 11.94546 11.95437 11.96313 11.97340
## [137] 11.98665 12.00254 12.02075 12.04095 12.06282 12.08602 12.11022 12.13511
## [145] 12.16034 12.18560 12.21055 12.23487 12.25823 12.28030 12.30075 12.31926
## [153] 12.33549 12.34913 12.36391 12.38337 12.40682 12.43359 12.46299 12.49436
## [161] 12.52701 12.56027 12.59346 12.62589 12.65690 12.68580 12.71192 12.73458
## [169] 12.75311 12.76681 12.77809 12.78973 12.80166 12.81379 12.82607 12.83841
## [177] 12.85074 12.86298 12.87507 12.88693 12.89847 12.90964 12.92035 12.93053
## [185] 12.94011 12.94901 12.95716 12.96448 12.97090 12.97635 12.98075 12.98402
## [193] 12.98610 12.98691 12.98637 12.98441 12.98096 12.97594 12.96820 12.95687
## [201] 12.94231 12.92490 12.90498 12.88293 12.85911 12.83388 12.80761 12.78065
## [209] 12.75337 12.72614 12.69932 12.67327 12.64835 12.62493 12.60337 12.58403
## [217] 12.56728 12.55020 12.52985 12.50657 12.48069 12.45255 12.42249 12.39083
## [225] 12.35791 12.32408 12.28965 12.25498 12.22038 12.18621 12.15278 12.12045
## [233] 12.08953 12.06037 12.03331 12.00867 11.98680 11.96802 11.95060 11.93265
## [241] 11.91425 11.89552 11.87655 11.85745 11.83830 11.81922 11.80030 11.78164
## [249] 11.76334 11.74550 11.72822 11.71160 11.69574 11.68075 11.66694 11.65450
## [257] 11.64329 11.63317 11.62402 11.61570 11.60806 11.60098 11.59433 11.58795
## [265] 11.58173 11.57553 11.56921 11.56263 11.55566 11.54817 11.54002 11.53108
## [273] 11.52120 11.51088 11.50070 11.49064 11.48071 11.47090 11.46123 11.45167
## [281] 11.44224 11.43293 11.42374 11.41467 11.40572 11.39688 11.38816 11.37955
## [289] 11.37105 11.36264 11.35431 11.34607 11.33793 11.32990 11.32201 11.31426
## [297] 11.30667 11.29926 11.29203 11.28499 11.27817 11.27158 11.26523 11.25914
## [305] 11.25331 11.24776 11.24251 11.23757 11.23296 11.22869 11.22473 11.22106
## [313] 11.21765 11.21449 11.21154 11.20879 11.20621 11.20379 11.20149 11.19929
## [321] 11.19718 11.19512 11.19309 11.19108 11.18915 11.18741 11.18583 11.18443
## [329] 11.18320 11.18214 11.18125 11.18052 11.17995 11.17954 11.17929 11.17920
## [337] 11.17926 11.17947 11.17982 11.18033 11.18097 11.18176 11.18269 11.18377
## [345] 11.18499 11.18637 11.18790 11.18959 11.19145 11.19346 11.19564 11.19800
## [353] 11.20052 11.20322 11.20609 11.20914 11.21238 11.21580 11.21940
#assign fits to a vector
both_trendc <- fit_bothc

#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax

#reassign dataframes (just to be safe)
work_bothc <- wrfc_both

#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date

#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
  plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
                    data = smooth_frame_bothc,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc,
                                  '</br> Median Log Copies: ', round(both_trendc, digits = 2)),
                    line = list(color = '#E7298A', size = 8, opacity = 0.65),
                    showlegend = FALSE) %>%
     layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
                    showlegend = FALSE,
                    opacity = 0.25,
                    hoverinfo = "text",
                    text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
                                  '</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
                                  '</br> Min Log Copies: ', round(both_yminc, digits = 2)),
                    name = "",
                    fillcolor = '#E7298A',
                    line = list(color = '#E7298A')) %>%
                layout(yaxis = list(title = "Total Log SARS CoV-2 Copies", 
                                 showline = TRUE,
                                 automargin = TRUE)) %>%
                layout(xaxis = list(title = "Date")) %>%
                layout(title = "WRF C") %>%
    plotly::add_segments(x = as.Date("2020-06-24"), 
                                          xend = as.Date("2020-06-24"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Bars Repoen",
                                          hoverinfo = "text",
                                          text = "</br> Bars Reopen",
                                                 "</br> 2020-06-24",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-07-09"), 
                                          xend = as.Date("2020-07-09"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "Mask Mandate",
                                          hoverinfo = "text",
                                          text = "</br> Mask Mandate",
                                                 "</br> 2020-07-09",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
    plotly::add_segments(x = as.Date("2020-08-20"), 
                                          xend = as.Date("2020-08-20"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> Classes Begin",
                                                 "</br> 2020-08-20",
                                          hoverinfo = "text",
                                          text = "Classes Begin",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
        plotly::add_segments(x = as.Date("2020-10-03"), 
                                          xend = as.Date("2020-10-03"), 
                                          y = ~min(both_yminc), yend = ~max(both_ymaxc),
                                          opacity = 0.35,
                                          name = "</br> First Home Football Game",
                                                 "</br> 2020-10-03",
                                          hoverinfo = "text",
                                          text = "First Home Football Game",
                                          showlegend = FALSE,
                                          line = list(color = "black", dash = "dash")) %>%
  plotly::add_markers(x = ~date, y = ~log_total_copies_both,
                      data = wrfc_both,
                       hoverinfo = "text",
                       showlegend = FALSE,
                       text = ~paste('</br> Date: ', date, 
                                     '</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
                       marker = list(color = '#E7298A', size = 6, opacity = 0.65))

p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")

save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")

save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")